package com.icngor.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.icngor.dao.UserDaoI;
import com.icngor.dao.impl.BaseDaoImpl;
import com.icngor.model.TUser;
import com.icngor.pageModel.DataGrid;
import com.icngor.pageModel.User;
import com.icngor.service.UserServiceI;
import com.icngor.utils.Encrypt;

// 该类使用userDao,未使用父类中的方法
@Service(value = "userService")
public class UserServiceImpl extends BaseDaoImpl<TUser> implements UserServiceI {

	private static final Logger logger = Logger.getLogger(UserServiceImpl.class);

	private UserDaoI userDao;

	public UserDaoI getUserDao() {
		return userDao;
	}

	@Autowired
	public void setUserDao(UserDaoI userDao) {
		this.userDao = userDao;
	}

	public static Logger getLogger() {
		return logger;
	}

	@Override
	public User save(User u) {
		TUser t = new TUser();
		BeanUtils.copyProperties(u, t, new String[] { "pwd" });
		t.setId(UUID.randomUUID().toString());
		t.setCreatetime(new Date());
		t.setPwd(Encrypt.e(u.getPwd()));
		userDao.save(t);
		BeanUtils.copyProperties(t, u);
		return u;
	}

	@Override
	public User login(User u) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", u.getName());
		params.put("pwd", Encrypt.e(u.getPwd()));
		TUser t = userDao.get("from TUser t where t.name=:name and t.pwd=:pwd", params);
		if (t != null) {
			return u;
		}
		return null;
	}

	@Override
	public DataGrid datagrid(User user) {
		DataGrid dg = new DataGrid();
		String hql = "from TUser t ";
		Map<String, Object> params = new HashMap<String, Object>();
		if (user.getName() != null && !user.getName().trim().equals("")) {
			hql += " where t.name like :name ";
			params.put("name", "%%"+user.getName()+"%%");
		}
		if (user.getOrder() != null) {
			hql += " order by " + user.getSort() + " " + user.getOrder();
		}
		List<TUser> l = userDao.find(hql, params, user.getPage(), user.getRows());
		List<User> lu = new ArrayList<User>();
		if (l != null && l.size() > 0) {
			for (TUser t : l) {
				User u = new User();
				BeanUtils.copyProperties(t, u);
				lu.add(u);
			}
		}

		dg.setTotal(userDao.count("select count(*) "+hql,params));
		dg.setRows(lu);
		return dg;
	}

	@Override
	public void remove(String ids) {
		String[] ida = ids.split(",");
		String hql = "delete TUser t where t.id in(";
		for(String s:ida){
			hql+="'"+s+"',";
		}
		hql = hql.substring(0, hql.lastIndexOf(","))+")";
		userDao.executeHql(hql);
	}

	@Override
	public User edit(User u) {
		TUser t = userDao.get(TUser.class, u.getId());
		u.setModifytime(new Date());
		BeanUtils.copyProperties(u, t, new String[]{"id","pwd"});
		return u;
	}
	
	

}
